//1.引入
const express = require('express');
const expressArtTemplate = require('express-art-template');
const moment = require('moment');
const url = require('url');
//2.创建app对象
let app = express();

//创建留言数据对象
var msg = [
    {name: 'jjjj', content: '在海边说', time: '2017-12-12-12-12-12'},
    {name: 'aaaa', content: '在海边说', time: '2018-12-12-12-12-12'},
    {name: 'wwww', content: '在海边说', time: '2019-12-12-12-12-12'},
    {name: 'yyyy', content: '在海边说', time: '2027-12-12-12-12-12'},
    {name: 'iiii', content: '在海边说', time: '2427-12-12-12-12-12'},
];
//3.配置
app.engine('html', expressArtTemplate);
app.use('/public', express.static('public'));
//4.路由
//#留言列表
app.get('/', function (req, res) {
   res.render('index.html', {
       msgs: msg
   })
});

//#留言添加
app.get('/add', function (req, res) {
    res.render('add.html')
});

//#留言添加处理
app.use('/doadd', function (req, res) {
    //数据处理
    //1.接受参数
    let obj = url.parse(req.url, true).query;
    //2.入库（压入）
    let data = {name: obj.name, content: obj.content, time: moment().format()};
    msg.push(data);

    //3.跳转
    res.redirect('/');
});

//5.启动服务
app.listen(8080, function () {
    console.log('系统成功')
});